package com.yanzhu.service.mapper;

import com.yanzhu.service.model.Video;
import com.yanzhu.service.model.mix.VideoHot;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: 方炎柱
 * @Description:
 * @Date: Created in 9:59 2018/1/19
 * @Modified:
 */
@Mapper
public interface VideoMapper {
    @Insert("insert into video(uid, path, image_path, duration, view, like_sum, video_time, tid) values(#{uid}, #{path}, #{image_path}, #{duration}, #{view}, #{like_sum}, #{video_time}, #{tid})")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = long.class)
    int insert(Video video);

    @Select("select v.id, v.uid, v.path, v.image_path, v.view, v.like_sum, v.video_time, u.nickname, u.avatar" +
            " from video v" +
            " left join user u on u.id = v.uid" +
            " where v.uid = #{uid}" +
            " order by v.video_time desc" +
            " limit #{page}, ${size}")
    List<VideoHot> selectByUid(@Param("uid") long uid, @Param("page") int page, @Param("size") int size);

    @Select("select v.id, v.uid, v.path, v.image_path, v.view, v.like_sum, v.video_time, u.nickname, u.avatar" +
            " from video v" +
            " left join user u on u.id = v.uid" +
            " where view > #{view}" +
            " order by v.view desc" +
            " limit #{page}, ${size}")
    List<VideoHot> queryHotVideo(@Param("view") int view, @Param("page") int page, @Param("size") int size);

    @Update("update video" +
            "  set view = view + 1" +
            "  where id = #{vid}")
    void updateVideoView(@Param("vid") long vid);

    @Update("update video" +
            "  set like_sum = like_sum + #{like_sum}" +
            "  where id = #{vid}")
    void updateVideoLike(@Param("vid") long vid, @Param("like_sum") int like_sum);

    @Update("update video" +
            "  set uid=#{uid}," +
            "  path=#{path}," +
            "  image_path=#{image_path}," +
            "  duration=#{duration}," +
            "  view=#{view}," +
            "  like_sum=#{like_sum}," +
            "  video_time=#{video_time}" +
            "  where id = #{id}")
    void updateVideo(Video video);

    @Select("select path from video where id = #{vid}")
    String selectVideoPathById(@Param("vid") long vid);

    @Select("select * from video where id = #{vid}")
    Video selectById(@Param("vid") long vid);

    @Delete("delete from video where id = #{vid}")
    int deleteVideoById(@Param("vid") long vid);

    @Delete("delete from video where uid=#{uid}")
    int deleteVideoAll(@Param("uid") long uid);

}
